package com.itheima.info.manger.studentservice;

import com.itheima.info.manger.dao.OtherStudentDao;
import com.itheima.info.manger.dao.StudentDao;
import com.itheima.info.manger.dao.StudentDao1;
import com.itheima.info.manger.domain.Student;

public class StudentService {
    //创建studentDao接收stu
        OtherStudentDao studentDao = new OtherStudentDao();
    //StudentDao调用findAllStudent()方法,返回学生数组.
    //Student[] students = studentDao.findAllStudent();

    public boolean addStudent(Student stu) {
        //接收studentDao的返回值
        return studentDao.addStudent(stu);
    }

    public boolean isExists(String id) {
        Student[] students = studentDao.findAllStudent();
        //遍历students学生对象,判断学生对象元素是否为空,并且如果students对象id和输入的id相同,则返回true,id已存在,否则返回false,id不存在.
        for (int i = 0; i < students.length; i++) {
            if (students[i] != null && students[i].getId().equals(id)){
                return true;
            }
        }
        return false;
    }

    public Student[] findAllStudent() {
        Student[] students = studentDao.findAllStudent();
        //初始化flag为false.
        boolean flag = false;
        //遍历students数组,如果数组中有至少一个元素,则将true赋值给flag,结束for循环return students数组;如果遍历数组中都为null则return null.
        for (int i = 0; i < students.length; i++) {
            if (students[i] != null){
                flag = true;
                break;
            }
        }
        if (flag){
            return students;
        }else {
            return null;
        }
    }
    //创建deleteStudent方法,将要删除的id传递给studentDao,并返回studentDao.deleteStudent(id)结果
    public boolean deleteStudent(String id) {
        return studentDao.deleteStudent(id);
    }
    //创建updateStudent方法,将新students
    public boolean updateStudent(Student students) {
        return studentDao.updateStudent(students);
    }
}
